GBML manual / Arrays

array_reverse

Turns the order of the places in target around, in place: the first place takes the value the last one held and the other way round, all the way to the middle. It changes target itself and makes no copy. Any kind of value can be reversed, text included.

Syntax

array_reverse(target)

Arguments

NameType
targetarray

Returns

Nothing.

Description

Turns the order of the places in target, an array, around. The first place takes the value the last one held, the last takes the first one's, and the ones in between swap the same way, all the way to the middle.

array_reverse(order);    // 1, 2, 3, 4 becomes 4, 3, 2, 1

It changes the array itself

array_reverse moves the values that are already in target. It does not make a new array, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with array_copy first and reverse the one you want turned around.

It turns around the places the array really has, the number array_length gives you. An array never gains or loses a place, so it has the same places afterwards.

Any kind of value can be reversed: numbers, text, structs, references. Nothing is compared, so there is no rule about what the array holds.

What happens in the awkward cases

You do thisWhat happens
reverse an array with one placenothing changes
reverse an array that has the same value more than onceworks as normal. Equal values are interchangeable, so you cannot tell which is which
reverse it twiceyou are back where you started
give it a listrefused when checked. A list has its own, list_reverse
use it where a value is neededrefused when checked. It gives nothing back

In a script

A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_reverse inside a script reverses the caller's array, whatever its size.

What it costs

Nothing is set aside: the values are swapped where they stand, one swap for every two places. No timing was measured.

Example

var order = array_create(4, 0);
order[0] = 1;
order[1] = 2;
order[2] = 3;
order[3] = 4;
array_reverse(order);    // order is now 4, 3, 2, 1
var first = order[0];    // 4

On the GBA

Reverses the array_length(target) places the array has. Reverse (the GBA module's own alias for std::reverse): it swaps the first item with the last, the second with the second-last, and so on towards the middle, in place - no copy and no extra memory, O(n) in how many items it turns around. Nothing is compared, so any kind of item works, text and structs included. With no items or one item there is nothing to swap and nothing happens.

See also: list_reversearray_sortarray_shufflearray_copyarray_lengthArraysData containers